package info

import (
	"service/utils"
	"sync"

	"log"
	"net/http"
)

type ComInfo struct {
	Status int         `json:"status"`
	Cpu    interface{} `json:"cpu"`
	Mem    interface{} `json:"mem"`
	Disk   interface{} `json:"disk"`
	Net    interface{} `json:"net"`
}

const (
	NUM_KB  = 1000.0000
	NUM_MIB = 1000000.0000
	NUM_GB  = 1000000000.0000
)

func GetHostInfo(w http.ResponseWriter, r *http.Request) {
	var wg sync.WaitGroup

	comInfo := ComInfo{}
	var cpuInfo interface{}
	var netInfo interface{}

	wg.Add(2)
	go func() {
		defer wg.Add(-1)
		cpuInfo, _ = GetCpuInfo()
		comInfo.Cpu = cpuInfo
	}()

	go func() {
		defer wg.Add(-1)
		netInfo, _ = GetNetInfo()
		comInfo.Net = netInfo
	}()

	memInfo, _ := GetMemInfo()
	diskInfo, _ := GetDiskInfo()

	comInfo.Mem = memInfo
	comInfo.Disk = diskInfo

	wg.Wait()
	comInfo.Status = 200
	re, err := utils.StructToJson(comInfo)
	if err != nil {
		log.Println(err)
		_, _ = w.Write([]byte(`{"status": 601, "msg":"转换错误!"}`))
	}

	w.Header().Add("Content-Type", "text/json")
	w.Write([]byte(re))

}
